

package com.hazelcast.internal.util.phonehome;

import javax.annotation.Nonnull;
import javax.annotation.concurrent.NotThreadSafe;
import java.net.URLEncoder;
import java.util.LinkedHashMap;
import java.util.Map;

import static java.nio.charset.StandardCharsets.UTF_8;

@NotThreadSafe
public class MetricsCollectionContext {
    private final Map<String, String> parameters = new LinkedHashMap<>();
    private String query;

    Map<String, String> getParameters() {
        return parameters;
    }

    /**
     * Collects the given {@code metric}. The value will be converted to string.
     */
    public void collect(@Nonnull Metric metric, @Nonnull Object value) {
        String parameter = metric.getQueryParameter();
        String valueStr = value.toString();

        if (parameters.containsKey(parameter)) {
            throw new IllegalArgumentException("Parameter " + parameter + " is already added");
        }

        parameters.put(parameter, valueStr);
        query = null;
    }

    String getQueryString() {
        if (query == null) {
            query = parameters.entrySet().stream().collect(StringBuilder::new, (sb, e) -> (sb.isEmpty() ? sb : sb.append('&')).append(e.getKey()).append('=').append(URLEncoder.encode(e.getValue(), UTF_8)), StringBuilder::append).toString();
        }
        return query;
    }
}

